package com.dai.zhenfujin.my.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.my.adapter.AccountAdapter;
import com.dai.zhenfujin.my.bean.AccountBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 账单页面
 */
public class AccountActivity extends BaseActivity {
    private TextView tvTitle;
    private ImageView imageView,iv_no_msg;
    private ListView listView;
    private AccountAdapter adapter;
    private List<AccountBean> beanList;

    private List<AccountBean.TradeListBean> trade_list = new ArrayList<>();
    private SmartRefreshLayout refreshLayout;
    private int page = 1;
    private int number = 10;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);

        initView();
        initListener();
        onRefresh();
        initRefresh();
      //  initData();
        beanList = new ArrayList<>();
        for (int i = 0; i <20 ; i++) {
          //  beanList.add(new AccountBean("买购服务","2018-01-02 09：54","-20"));
        }
    }

    private void initRefresh() {
        page = 1;
        getWalletList(true);
    }



    private void onRefresh() {
        refreshLayout.setOnRefreshListener(refreshLayout -> refreshLayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                page = 1;
                getWalletList(true);
                refreshLayout.finishRefresh();
                refreshLayout.resetNoMoreData();
            }
        }, 1500));

        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                getWalletList(false);
            }
        });
    }


    private void getWalletList(boolean isRefresh) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("user_id","");
        params2.put("page", page + "");
        params2.put("number", number + "");
        params.put("req_bd",  params2);
        HttpClient.post(this, Constants.TRADELIST, params, new CallBack<AccountBean>() {
            @Override
            public void onSuccess(AccountBean resultBean) {
                if (isRefresh) {
                    trade_list.clear();
                    page = 2;
                } else {
                    page++;
                    refreshLayout.finishLoadmore();
                }
                if (resultBean.getTrade_list().size() > 0) {
                    trade_list.addAll(resultBean.getTrade_list());
                    adapter.notifyDataSetChanged();
                    if (page == 2) {
                        iv_no_msg.setVisibility(View.GONE);
                    }
                } else {
                    refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                    if (page == 2) {
                       // iv_no_msg.setVisibility(View.VISIBLE);
                      //  view_line.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    private void initData() {

    }

    private void initListener() {
        imageView.setOnClickListener(v -> finish());
    }

    private void initView() {
        tvTitle = (TextView)findViewById(R.id.title_textview);
        tvTitle.setText("账单");
        imageView=(ImageView)findViewById(R.id.title_imageview);
        listView = (ListView)findViewById(R.id.account_listview);
        refreshLayout = findViewById(R.id.refreshLayout);
        iv_no_msg = findViewById(R.id.iv_no_msg);

        adapter = new AccountAdapter(this,trade_list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                startActivity(new Intent(AccountActivity.this,AccountDetailsActivity.class)
                               .putExtra("trade_id",trade_list.get(position).getTrade_id()));
            }
        });
    }

//    private void showDateDialog() {
//
//        view = View.inflate(this,R.layout.date_diaog,null);
//        np1 = view.findViewById(R.id.np1);
//        np2 = view.findViewById(R.id.np2);
////        np3 = view.findViewById(R.id.np3);
//
//        //获取当前日期
//        Calendar c = Calendar.getInstance();
//        final int year = c.get(Calendar.YEAR);
//        final int month = c.get(Calendar.MONTH)+1;//月份是从0开始算的
//        final int day = c.get(Calendar.DAY_OF_MONTH);
//
//        //设置年份
//        np1.setMaxValue(2999);
//        np1.setValue(year);//中间参数  设置默认值
//        np1.setMinValue(1900);
//
//        //设置月份
//        np2.setMaxValue(12);
//        np2.setValue(month);
//        np2.setMinValue(1);
//
////        // 设置天数
////        np3.setMaxValue(31);
////        np3.setValue(day);
////        np3.setMinValue(1);
//
//        //年份滑动监听
//        np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
//            @Override
//            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
//                //平年润年判断
//                if(newVal%4 == 0){
//                    maxDay = 29;
//                }else {
//                    maxDay = 28;
//                }
////                //设置天数的最大值
////                np3.setMaxValue(maxDay);
//            }
//        });
//
//        //月份滑动监听
//        np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
//            @Override
//            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
//                //月份判断
//                switch (newVal){
//                    case 2:
//                        if(np1.getValue()%4==0){
//                            maxDay=29;
//                        }else{
//                            maxDay=28;
//                        }
//                        break;
//                    case 1:
//                    case 3:
//                    case 5:
//                    case 7:
//                    case 8:
//                    case 10:
//                    case 12:
//                        maxDay=31;
//                        break;
//                    default:
//                        maxDay=30;
//                        break;
//                }
////                //设置天数的最大值
////                np3.setMaxValue(maxDay);
//            }
//        });
//
//        new AlertDialog.Builder(this).setTitle("请选择时间")
//                .setView(view)
//                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        int years = np1.getValue();
//                        int months = np2.getValue();
////                        int days = np3.getValue();
//                        et.setText(years + "年" + months + "月");
////                        et.setText(years + "年" + months + "月" + days + "日");
//                        dialog.dismiss();
//                    }
//                })
//                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        dialog.dismiss();
//                    }
//                })
//                .show();
//    }
}

